package br.com.intelsys.crawler;

import java.util.HashSet;
import java.util.Set;
import java.util.regex.Pattern;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import edu.uci.ics.crawler4j.crawler.Page;
import edu.uci.ics.crawler4j.parser.HtmlParseData;
import edu.uci.ics.crawler4j.url.WebURL;

public class BasicControllerData implements ControllerData {
	private static final Log log = LogFactory.getLog(CrawlerBrowser.class);
	private final String url;
	private String urlId;
	private final static Pattern FILTERS = Pattern.compile(".*(\\.(css|js|bmp|gif|jpe?g" + "|png|tiff?|mid|mp2|mp3|mp4"
			+ "|wav|avi|mov|mpeg|ram|m4v|pdf" + "|rm|smil|wmv|swf|wma|zip|rar|gz))$");
	private Set<CrawledPage> crawledPageSet;

	public BasicControllerData(String url) {
		this.url = url;
		crawledPageSet = new HashSet<CrawledPage>();
	}

	String getUrl() {
		return url;
	}

	public void setUniqueUrlId(String urlId) {
		this.urlId = urlId;
	}

	public boolean shouldVisit(WebURL webUrl) {
		String href = webUrl.getURL().toLowerCase();
		return !FILTERS.matcher(href).matches() && (href.startsWith(url));
	}

	public Set<CrawledPage> getCrawledPageSet() {
		return crawledPageSet;
	}

	public void pageFound(Page page) {
		String url = page.getWebURL().getURL();
		if (!crawledPageSet.contains(url)) {
			if (log.isDebugEnabled()) {
				log.debug("Start crawlling [" + urlId + "]: [docId: " + page.getWebURL().getDocid() + "] " + url);
			}
			HtmlParseData htmlParseData = (HtmlParseData) page.getParseData();
			crawledPageSet.add(new CrawledPage(url, htmlParseData.getHtml()));
		}
		/*
		 	if (page.getParseData() instanceof HtmlParseData) {
			HtmlParseData htmlParseData = (HtmlParseData) page.getParseData();
			String text = htmlParseData.getText();
			String html = htmlParseData.getHtml();
			List<WebURL> links = htmlParseData.getOutgoingUrls();

			count++;

			System.out.println("count: " + count);
			System.out.println("Text length: " + text.length());
			System.out.println("Html length: " + html.length());
			System.out.println("Number of outgoing links: " + links.size());
			CrawledPage cp = new CrawledPage(url, html);

			Checker c = new XSSSingleChecker();
			resultList.add(c.doCheck(cp));

		}

		 */
	}
}
